//
//  HomeSonViewController.m
//  pocketNews
//
//  Created by DS on 2018/10/26.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "HomeSonViewController.h"
#import "HomeTableViewCell.h"
#import "HomeSonModel.h"

#import "WBWebViewController.h"

#import "GiFHUD.h"

#import "CLRefreshHeader.h"

#import "CLGifLoadView.h"

@interface HomeSonViewController ()<UITableViewDelegate,UITableViewDataSource>

Strong UITableView * tableView;

Strong NSMutableArray * dataArr;

Assign int page;

@end


//网络
@implementation HomeSonViewController (NetWorking)
//新闻接口
- (void)getData:(int)page{
    
   // [GiFHUD show];
    
    [self showprogressHUD];
    NSDictionary *parameters = @{
                                 
                                 @"type":self.type,
                                 
                                 @"page":[NSString stringWithFormat:@"%d",page],
                                 
                                 @"key":@"c6e4817b12d373379556ffb70a5af0cb"
                                 
                                 }.mutableCopy;
    
    [[AFAppDotNetAPIClient sharedClient] POST:@"http://v.juhe.cn/toutiao/index" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
     
        //[GiFHUD dismiss];
        
        if ([responseObject[@"result"][@"stat"] intValue] == 1) {
          
            for (NSDictionary * dic in responseObject[@"result"][@"data"]) {
                
                if (page == 1) {
                    [self.dataArr removeAllObjects];
                    self.page = 1;
                }
                
                self.page ++;
                
                HomeSonModel * model = [[HomeSonModel alloc] init];
                
                [model HomeSonModelWithDic:dic];
                
                [self.dataArr addObject:model];
                
            }
           
            [self.view addSubview:self.tableView];
            [self.tableView reloadData];
        }else{
            
            ShowMessage(responseObject[@"reason"]);
            
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        //[GiFHUD dismiss];
        [self hiddenProgressHUD];
        [self showError:error];
        
        NSLog(@"%@",error);
    }];
}
@end



@implementation HomeSonViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    
    self.navigationController.navigationBar.hidden = YES;
    
}



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [GiFHUD setGifWithImageName:@"pika.gif"];
    
     [self getData:2];
    
   
}

- (void)viewDidDisappear:(BOOL)animated{
    
    [super viewDidDisappear:animated];
    
    self.navigationController.navigationBar.hidden = NO;
    
    
}

//tabelView
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 35 - height49 - UNIT_HEIGHT(100)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.tableHeaderView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0.01)];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        
        _tableView.mj_header =  [CLRefreshHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
        
        self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return _tableView;
}

- (void)loadNewData{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        
        [self getData:2];
        
        [self.tableView.mj_header endRefreshing];
    });
}

- (void)loadMoreData{
    
    [_tableView.mj_footer beginRefreshing];
    
    [self getData:self.page];
    
    [_tableView.mj_footer endRefreshing];
    
    
}

#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    

    
    return self.dataArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeTableViewCell * cell = [HomeTableViewCell cellForTableView:tableView];
    
    cell.model = self.dataArr[indexPath.section];
    
    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return UNIT_HEIGHT(130);

}




- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    if (section == 4) {
        
        return 0;
    }
    
    return UNIT_HEIGHT(10);
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HomeSonModel * model = self.dataArr[indexPath.section];
    
    
    WBWebViewController *webVC = [[WBWebViewController alloc] init];
    webVC.urlString = model.url;
    webVC.isWKWebView = YES;
    //webVC.title = @"2345";
    
    webVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:webVC animated:YES];
   
  
}

#pragma mark -- 懒加载

- (NSMutableArray *)dataArr{
    
    if (!_dataArr) {
        
        _dataArr = @[
                     
                     ].mutableCopy;
    }
    
    return _dataArr;
    
}


@end
